package cn.xu.framework.concurrent.lock;

import java.util.concurrent.Callable;

/**
 * @Author xuguofei clark2021@qq.com || WX clark_2023
 * @Date 2024/3/8
 * @Desc TODO
 **/
public class ConcurrentSafeExecutor {

    public static <T> T exe(Callable<Boolean> lockCall,
                     Callable<Void> unLockCall,
                     Callable<T> coreCall,
                     Callable<Void> lockFailCall) throws Exception {
        Boolean lockFlag = true;
        try{
            lockFlag = lockCall.call();
            if (!lockFlag){
                lockFailCall.call();
                return null;
            }
            return coreCall.call();
        } finally {
            unLockCall.call();
        }
    }

    public static void main(String[] args) throws Exception {
        try {
            new ConcurrentSafeExecutor().exe(() -> {
                System.out.println(444);
                return true;
            }, () -> {
                System.out.println(333);
                return null;
            }, () -> {
                System.out.println(222);
                throw new NumberFormatException("ceesss");
            }, () -> {
                System.out.println(111);
                return null;
            });
        } catch (Exception e) {
            System.out.println(e.getClass());
            System.out.println(e.getMessage());
        }
    }
}
